<!--
 * @Author: your name
 * @Date: 2022-01-22 14:39:34
 * @LastEditTime: 2022-01-27 21:27:05
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \json-serveg:\桌面2\VueStudy\vue-ego\vue-ego\src\views\Login\Login.vue
-->
<template>
  <div class="body">
      <div class="title">
        <div class="logo">
          <img src="../../assets/logo.png" height="120px" alt="">
        </div>
        <!-- <div>{{ info }}</div> -->
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="账号" prop="username">
            <el-input type="username" v-model="ruleForm.username" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
          </el-form-item>
          
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>

      </div>

  </div>
</template>

<script>
import {mapMutations} from 'vuex'
import jwt from 'jwt-decode'
  export default {
    data() {
      
      var validateUser = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入账号'));
        } else {
          callback();
        }
      };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          callback();
        }
      };
      
      return {
        // info:'',
        ruleForm: {
          username: 'admin',
          password: '123'
        },
        rules: {
          username: [
            { validator: validateUser, trigger: 'blur' }
          ],
          password: [
            { validator: validatePass, trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      ...mapMutations('loginModule',['SETUSER']), // loginModule模块的名字,SETUSER模块里的方法
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let {username,password} = this.ruleForm
            console.log(username,password);
            //如果为true.登录接口
            this.$api.getLogin({username,password}).then((res) => {
              // console.log(res.data);
              if (res.data.status === 200) {
                // 登录成功后保存信息到vuex
                // jwt(res.data.data)
               let obj = {
                 user:jwt(res.data.data).username,
                 token:res.data.data
               }
              this.SETUSER(obj)
              //储存本地持久化
              localStorage.setItem('user',JSON.stringify(obj))
              //跳转
              this.$router.push('/')

              } else {
                // this.info= '账号密码错误'
                  this.$message({
                  message: '你输入的账号密码错误',
                  type: 'error'
                });
           
              }
              // console.log();

            })

          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>

<style lang="less" scoped>

.body{
   position: fixed;
  left: 0;
  top: 0;
  bottom: 0;
  right: 0;
  background: #104468;
}
.title{
  width: 400px;
  height: 260px;
  padding: 20px;
  margin: 0 auto;
  margin-top: 200px;
  border-radius: 10px;
  padding: 50px 30px;
  border: 1px solid #eee;
  background: #fff;
  position: relative;
}
.logo{
  position: absolute;
  width: 120px;
  height: 120px;
  background: #f7f7f7;
  border-radius: 50%;
  overflow: hidden;
  top:-80px;
  left:50%;
  margin-left: -60px;
  padding:8px;
  img{
    border-radius: 50%;
    background: #fff;
  }
}

</style>